<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once 'Savant2/Plugin.php';

class Savant2_Plugin_leagueman_get_match_event extends Savant2_Plugin{

	function plugin($project, $match, $eventtype){
		global $database, $Itemid;
		
		switch($eventtype){
			case "cnt_goals":
				return $this->display_goals($match, 2, $Itemid);
			break;
			
			case "cnt_own_goals":
				return $this->display_goals($match, 3, $Itemid);
			break;
			
			case "cnt_penalties":
				return $this->display_goals($match, 4, $Itemid);
			break;
			
			case "cnt_yellow":
				return $this->display_goals($match, 8, $Itemid);
			break;
			
			case "cnt_yellow_red":
				return $this->display_goals($match, 9, $Itemid);
			break;
			
			case "cnt_red":
				return $this->display_cards($match, 10, $Itemid);
			break;
		}
	}
	
	function query($match, $eventtype){
		global $database;
		
		$query = "SELECT e.*, p.player_id, p.player_name
					FROM #__leagueman_event e
			  INNER JOIN #__leagueman_playermap pm ON pm.playermap_id = e.event_playermap
			  INNER JOIN #__leagueman_player p ON pm.playermap_player = p.player_id
			  	   WHERE e.event_match = $match->match_id AND e.event_type = $eventtype
				ORDER BY e.event_minute";
		$database->setQuery($query);
		$rows = $database->loadObjectList();
		
		return $rows;
	}
	
	function display_goals($match, $eventtype, $Itemid){
		
		$rows = $this->query($match, $eventtype);
		
		$arr = array();
		foreach($rows AS $row){
			$href = sefRelToAbs("index.php?option=com_leagueman&func=playerinfo&pid=" . $row->player_id);
			
			$link = "<a href=\"" . $href . "\">" . $row->player_name . "</a>";
			
			$arr[] = $row->event_text_short . "&nbsp;" . $link . "&nbsp;" . sprintf("(%s.)", $row->event_minute);
		}
		
		return implode(", ", $arr);
		
	}
	
	function display_cards($match, $eventtype, $Itemid){
		
		$rows = $this->query($match, $eventtype);
		
		$arr = array();
		foreach($rows AS $row){
			$href = sefRelToAbs("index.php?option=com_leagueman&func=playerinfo&pid=" . $row->player_id);
			
			$link = "<a href=\"" . $href . "\">" . $row->player_name . "</a>";
			
			$arr[] = $link . "&nbsp;" . sprintf("(%s.)", $row->event_minute);
		}
		
		return implode(", ", $arr);
	}
}
?>